package com.wuseguang.springcloud.controller;

import com.wuseguang.springcloud.Classcardservice.ClassCardService;
import com.wuseguang.springcloud.Filter.RequestParameterWrapper;
import com.wuseguang.springcloud.entity.AttendGroup;
import com.wuseguang.springcloud.entity.AttendShift;
import com.wuseguang.springcloud.service.AttendTwoService;
import com.wuseguang.springcloud.util.AttendExcelUtils;
import com.wuseguang.springcloud.util.CodeUtil;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @描述 校/班务管理-->考勤管理
 * @创建人 zm
 * @创建时间 2020/8/3 12:13
 */
@RestController
@RequestMapping("/attend")
public class AttendTwoController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    private AttendTwoService attendService;

    //推送接口
    @Autowired
    private ClassCardService classCardService;

    /**
     * @描述 查询所有班次
     * @创建人 zm
     * @创建时间 2020/2/5
     */
    @RequestMapping("/queryShiftAll")
    public Result queryAttendShiftAll(RequestParameterWrapper request) {
        String schoolId = request.getParam("schoolId");
        logger.info("school_id={}", schoolId);
        return new Result(attendService.queryAttendShiftAll(schoolId));
    }


    /**
     * @描述 根据id 查班次详情
     * @创建人 zm
     * @创建时间 2020/2/5
     */
    @RequestMapping("/queryShiftById")
    public Result queryAttendShiftById(RequestParameterWrapper request,
                                       @RequestParam(value = "id") Integer id) {//班次id
        String schoolId = request.getParam("schoolId");
        logger.info("school_id={}", schoolId);
        return new Result(attendService.queryAttendShiftById(schoolId, id));
    }

    /**
     * @描述 修改班次
     * @创建人 zm
     * @创建时间 2020/2/5
     */
    @RequestMapping("/updateShiftById")
    public Result updateAttendShiftById(RequestParameterWrapper request,
                                        @RequestBody AttendShift attendShift) {//班次
        String schoolId = request.getParam("schoolId");
        logger.info("school_id={},AttendShift={}", schoolId, attendShift.toString());
        return attendService.updateAttendShiftById(schoolId, attendShift);
    }


    /**
     * @描述 添加班次
     * @创建人 zm
     * @创建时间 2020/2/5
     */
    @RequestMapping("/addShiftById")
    public Result addAttendShiftById(RequestParameterWrapper request,
                                     @RequestBody AttendShift attendShift) {//班次
        String schoolId = request.getParam("schoolId");
        logger.info("school_id={},AttendShift={}", schoolId, attendShift.toString());
        return attendService.addAttendShiftById(schoolId, attendShift);
    }

    /**
     * @描述 删除班次
     * @创建人 zm
     * @创建时间 2020/2/5
     */
    @RequestMapping("/delShiftById")
    public Result delAttendShiftById(RequestParameterWrapper request,
                                     @RequestParam(value = "id") Integer id) {//班次id
        String schoolId = request.getParam("schoolId");
        logger.info("school_id={}", schoolId);
        return attendService.delAttendShiftById(schoolId, id);
    }

    /**
     * @描述 组合查询所有考勤组详情
     * @创建人 zm
     * @创建时间 2020/2/5
     */
    @RequestMapping("/queryGroupAll")
    public Result queryAttendGroupAll(RequestParameterWrapper request,
                                      @RequestParam(value = "name", required = false) String name) {//输入名称查询
        String schoolId = request.getParam("schoolId");
        logger.info("school_id={}", schoolId);
        return new Result(attendService.queryAttendGroupAll(schoolId, name));
    }


    /**
     * @描述 查该学校所有考勤组
     * @创建人 zm
     * @创建时间 2020/2/5
     */
    @RequestMapping("/queryAttendGroup")
    public Result queryAttendGroup(RequestParameterWrapper request) {
        String schoolId = request.getParam("schoolId");
        //管理员
        String admin = request.getParam("admin");
        //教师id
        String teacherId = request.getParam("teacherId");
        logger.info("school_id={}", schoolId);
        return new Result(attendService.queryAttendGroup(schoolId, admin, teacherId));
    }


    /**
     * 根据考勤组id查询对应年级/班级信息
     *
     * @Author: zm
     * @Date: 2020/8/3
     */
    @RequestMapping("/queryGroupClass")
    public Result queryAttendGroupClass(@RequestParam(value = "id") Integer id) {//考勤组id
        return new Result(attendService.queryAttendGroupClass(id));
    }


    /**
     * @Description: 删除考勤组
     * @Param: [request, id]
     * @return: com.wuseguang.springcloud.util.Result
     * @Author: zm
     * @Date: 2020/8/3
     */
    @RequestMapping("/delGroupById")
    public Result delAttendGroupById(RequestParameterWrapper request,
                                     @RequestParam(value = "id") Integer id) {//考勤组id
        String schoolId = request.getParam("schoolId");
        logger.info("school_id={}", schoolId);
        String s = attendService.attendGroupClassid(id, "0");
        int i = attendService.delAttendGroupById(schoolId, id);
        if (i > 0) {
            classCardService.classesAndCards(s);
            return new Result();
        }
        return new Result(CodeUtil.DELETE_FAILED);
    }

    /**
     * @描述 根据id查询考勤组详细内容
     * @创建人 zm
     * @创建时间 2020/2/5
     */
    @RequestMapping("/queryGroupById")
    public Result queryAttendGroupById(RequestParameterWrapper request,
                                       @RequestParam(value = "id") Integer id) {//考勤组id
        String schoolId = request.getParam("schoolId");
        logger.info("school_id={}", schoolId);
        return new Result(attendService.queryAttendGroupById(schoolId, id));
    }


    @RequestMapping("/attendString")//测试使用
    public Result queryAttendGroupById() {//考勤组id
        attendService.attendString();
        return new Result();
    }

    /**
     * @描述 修改考勤组
     * @创建人 zm
     * @创建时间 2020/2/5
     */
    @RequestMapping("/updateGroup")
    public Result updateAttendGroup(RequestParameterWrapper request,
                                    @RequestBody AttendGroup attendGroup) {//考勤组
        String schoolId = request.getParam("schoolId");
        logger.info("school_id={},attendGroup={}", schoolId, attendGroup.toString());
        String s = attendService.attendGroupClassid(attendGroup.getId(), attendGroup.getClassids());
        int i = attendService.updateAttendGroup(schoolId, attendGroup);
        if (i > 0) {
            logger.info("updateAttendGroup->推送,attendGroup.getClassids()={},s={}", attendGroup.getClassids(), s);
            classCardService.classesAndCards(s);
            classCardService.classesAndCards(attendGroup.getClassids());
            return new Result();
        }
        return new Result(CodeUtil.MODIFICATION_FAILED);
    }

    /**
     * @描述 添加考勤组
     * @创建人 zm
     * @创建时间 2020/2/5
     */
    @RequestMapping("/addGroup")
    public Result addAttendGroup(RequestParameterWrapper request,
                                 @RequestBody AttendGroup attendGroup) {//考勤组
        String schoolId = request.getParam("schoolId");
        logger.info("school_id={},attendGroup={}", schoolId, attendGroup.toString());
        int i = attendService.addAttendGroup(schoolId, attendGroup);
        if (i > 0) {
            logger.info("addGroup->推送,addGroup.getClassids()={}", attendGroup.getClassids());
            classCardService.classesAndCards(attendGroup.getClassids());
            return new Result();
        }
        return new Result(CodeUtil.ADD_FAILURE);
    }


    @RequestMapping("/queryAttendAll")
    public Result queryAttendAll(RequestParameterWrapper request,
                                 @RequestParam(value = "attend_groupid") String attend_groupid,
                                 @RequestParam(value = "starttime") String starttime,
                                 @RequestParam(value = "endtime") String endtime,
                                 @RequestParam(value = "classids", required = false) String classids) {//格式 "1,2,3,5"
        String schoolId = request.getParam("schoolId");
        logger.info("school_id={}", schoolId);
        Map<String, Object> mapList = attendService.queryAttendAll(attend_groupid, schoolId, starttime, endtime, classids);
        return new Result(mapList);
    }

    /**
     * @描述 导出考勤数据
     * @创建人 zm
     * @创建时间 2020/2/6
     */
    @RequestMapping("/downloadAttend")
    public void downloadAttend(RequestParameterWrapper request, HttpServletResponse response,
                               @RequestParam(value = "sid", required = false) String sid,//不为null 时查全校
                               @RequestParam(value = "attend_groupid") String attend_groupid,
                               @RequestParam(value = "starttime") String starttime,
                               @RequestParam(value = "endtime") String endtime,
                               @RequestParam(value = "classids", required = false) String classids) throws IOException {
        String schoolId = request.getParam("schoolId");
        logger.info("school_id={}", schoolId);
        Map<String, Object> map = attendService.downloadAttend(request, response, sid, attend_groupid, starttime, endtime, classids);
        List<String> weeklist = (List<String>) map.get("weeklist");
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) map.get("attendlist");
        AttendExcelUtils.downexcle(response, mapList, weeklist, "考勤");
    }

    @RequestMapping("/saveAttend2")
    public Result saveAttend(RequestParameterWrapper request) {
        logger.info("保存考勤");
        String schoolId = request.getParam("schoolId");
        attendService.executeInternal();
        return new Result();
    }

}
